#!/bin/bash -
#=======================================================================
#
#          FILE: cp.sh
#         USAGE: ./cp.sh <src> <dst>
#   DESCRIPTION: Copy file with progress bar (similar to wget)
#  REQUIREMENTS: cp, strace
#          BUGS: N/A
#         NOTES: N/A
#        AUTHOR: Ali Ayoub
#         EMAIL: ali@mellanox.com
#       COMPANY: N/A
#       CREATED: 09.17.2010-15:46:13
#      REVISION: 1.0
#=======================================================================

USAGE="Usage: $0 <src> <dst>"
EXAMPLE="Example: $0 /tmp/ali.iso /dev/null"
if [ $# != 2 ]; then
        echo $USAGE
        echo $EXAMPLE
        exit 1
fi

cp_p()
{
   strace -q -ewrite cp -- "${1}" "${2}" 2>&1 \
      | awk '{
        count += $NF
            if (count % 10 == 0) {
               percent = count / total_size * 100 + 1
               printf "%3d%% [", percent
               for (i=0;i<=percent;i++)
                  printf "="
               printf ">"
               for (i=percent;i<100;i++)
                  printf " "
               printf "]\r"
            }
         }
         END { print "" }' total_size=$(stat -c '%s' "${1}") count=0
}


cp_p $1 $2
echo $2/`basename $1` copied
